package com.runjian.rundo.iot.agent.business.control.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.runjian.rundo.iot.agent.business.config.BaseConfigurationProperties;
import com.runjian.rundo.iot.agent.business.control.cache.DeviceControlCache;
import com.runjian.rundo.iot.agent.business.control.cache.impl.MemoryDeviceControlCache;
import com.runjian.rundo.iot.agent.business.control.executor.impl.DefaultDeviceCommandExecutor;
import com.runjian.rundo.iot.agent.business.control.publisher.DeviceControlRespMessageProcessor;
import com.runjian.rundo.iot.agent.business.publisher.impl.KafkaMessagePublisher;
import com.runjian.rundo.iot.agent.common.codec.DeviceMessageCodec;
import com.runjian.rundo.iot.agent.common.manager.DeviceManager;
import com.runjian.rundo.iot.agent.common.session.manager.SessionManager;
import com.runjian.rundo.iot.agent.common.thread.ScheduledExecutorFactory;

import lombok.Getter;
import lombok.Setter;

/**
 * 控制配置属性
 *
 * @author CaoQingHua
 */
@Setter
@Getter
@EnableConfigurationProperties(ControlConfigurationProperties.class)
@Configuration
public class ControlAutoConfiguration {

    @Bean
    public DeviceControlCache deviceCache() {
        return new MemoryDeviceControlCache();
    }

    @Bean
    @ConditionalOnClass({DeviceMessageCodec.class, SessionManager.class})
    public DefaultDeviceCommandExecutor deviceCommandExecutor(ControlConfigurationProperties configurationProperties,
        ScheduledExecutorFactory scheduledExecutorFactory, DeviceControlCache deviceCache, DeviceMessageCodec codec,
        SessionManager deviceSessionManager, DeviceManager deviceManager) {
        return new DefaultDeviceCommandExecutor(configurationProperties, scheduledExecutorFactory, codec, deviceSessionManager, deviceCache,
            deviceManager);
    }

    @Bean
    @ConditionalOnClass({BaseConfigurationProperties.class, KafkaMessagePublisher.class})
    public DeviceControlRespMessageProcessor deviceControlRespMessageProcessor(ControlConfigurationProperties configurationProperties,
        KafkaMessagePublisher kafkaMessagePublisher) {
        return new DeviceControlRespMessageProcessor(configurationProperties, kafkaMessagePublisher);
    }

}
